﻿Imports System.Data.OleDb
Public Class addNewRestaurant
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_Submit_Click(sender As Object, e As EventArgs) Handles btn_Submit.Click

        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(fup_RPicture.FileName)
        If fup_RPicture.HasFile Then
            fup_RPicture.SaveAs(Server.MapPath("pictures") & "/" & newFileName)
        End If



        Dim sql As String = "INSERT INTO Restaurants (RName,RAddress,RWebsite,RPicture,AveragePriceInPounds,Tel,MichelinStars) VALUES (@f1,@f2,@f3,@f4,@f5,@f6,@f7)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("RestaurantConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", tb_RName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_RAddress.Text)
        cmd.Parameters.AddWithValue("@f3", tb_RWebsite.Text)
        cmd.Parameters.AddWithValue("@f4", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f5", tb_AveragePrice.Text)
        cmd.Parameters.AddWithValue("@f6", tb_RTel.Text)
        cmd.Parameters.AddWithValue("@f7", rbl_MichelinStars.SelectedValue)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("list.aspx")

    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function
End Class